package com.sws.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sws.mapping.PermissionMapper;
import com.sws.mapping.UserMapper;
import com.sws.model.UserModel;
import com.sws.model.bean.B2SUserBean;
import com.sws.model.bean.C2SUserBean;
import com.sws.model.bean.S2CUserBean;
import com.sws.service.UserService;
import com.sws.utils.EhCache;
import com.sws.utils.ThreadPool;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper mapper;
	@Autowired
	private PermissionMapper permissionMapper;

	@Override
	public S2CUserBean login(C2SUserBean user) {
		// TODO Auto-generated method stub
		S2CUserBean bean = mapper.login(user);
		if (bean != null) {
			ThreadPool.getDefaultThreadPool().execute(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					List<String> permissions = permissionMapper.getPermission4Cache(bean.getId());
					EhCache.getInstance().put(bean.getId(), permissions);
				}
			});
		}
		return bean;
	}

	@Override
	public void addStudent(List list) {
		// TODO Auto-generated method stub
		for (UserModel model : (List<UserModel>)list) {
			mapper.uploadUser(model);
		}
	}

	@Override
	public void addStudent(UserModel model) {
		// TODO Auto-generated method stub
		mapper.uploadUser(model);
	}

	@Override
	public int deleteStudent(Integer user) {
		// TODO Auto-generated method stub
		return mapper.deleteByPrimaryKey(user);
	}

	@Override
	public void modifyStudent(UserModel model) {
		// TODO Auto-generated method stub
		mapper.updateUser(model);
	}

}
